﻿Imports System.Math
Imports System.Drawing

Public Class Form2
    Dim coefficients(0 To 3, 0 To 15, 0 To 15) As Double
    Dim pixelvaluesY(0 To 15, 0 To 15) As Double
    Dim pixelvaluesU(0 To 15, 0 To 15) As Double
    Dim pixelvaluesV(0 To 15, 0 To 15) As Double
    Dim bit As New Bitmap("D:\Test Projects\New\image compare\I12P10347021-0001.jpg")
    Dim NonIndexedImage As New Bitmap(bit.Width, bit.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)

    Public Sub getpixels()
        Dim x As Integer
        Dim y As Integer

        For x = 0 To 15
            For y = 0 To 15
                pixelvaluesY(x, y) = (0.299 * bit.GetPixel(x, y).R) + (0.5815 * bit.GetPixel(x, y).G) + (0.114 * bit.GetPixel(x, y).B) 'gety(bit.GetPixel(x + 100, y + 100).ToArgb)
                pixelvaluesU(x, y) = getu(bit.GetPixel(x + 100, y + 100).ToArgb)
                pixelvaluesV(x, y) = getv(bit.GetPixel(x + 100, y + 100).ToArgb)
            Next y
        Next x

    End Sub

    Public Sub putpixels()
        Dim x As Integer
        Dim y As Integer
        Dim rc As Integer
        Dim gc As Integer
        Dim bc As Integer

        For x = 0 To 15
            For y = 0 To 15
                rc = yuvtor(pixelvaluesY(x, y), pixelvaluesV(x, y))
                gc = yuvtog(pixelvaluesY(x, y), pixelvaluesU(x, y), pixelvaluesV(x, y))
                bc = yuvtob(pixelvaluesY(x, y), pixelvaluesU(x, y))

                NonIndexedImage.SetPixel(x, y, Color.AliceBlue) ' Color.FromArgb(rc, gc, bc))
            Next y

        Next x

    End Sub

    Public Function a(value As Integer) As Double

        If value = 0 Then
            a = 1 / Sqrt(2)
        Else
            a = 1
        End If

    End Function

    Public Sub doDCT(matrixno As Integer)

        Dim u As Integer
        Dim v As Integer
        Dim x As Integer
        Dim y As Integer
        Dim pi As Double
        Dim tempcalc As Double


        pi = 3.141592653589159

        For u = 0 To 15
            For v = 0 To 15

                tempcalc = 0
                For x = 0 To 15
                    For y = 0 To 15

                        tempcalc = tempcalc + (pixelvaluesY(x, y)) * _
                            Cos((((2 * x) + 1) * u * pi) / 32) * _
                            Cos((((2 * y) + 1) * v * pi) / 32)

                    Next y
                Next x

                coefficients(matrixno, u, v) = a(u) * a(v) * tempcalc / 8
            Next v
        Next u
        Print_DCT()
    End Sub

    Public Sub doiDCT(matrixno As Integer)
        Dim u As Integer
        Dim v As Integer
        Dim x As Integer
        Dim y As Integer
        Dim pi As Double
        Dim tempcalc As Double


        pi = 3.141592653589159


        For x = 0 To 15
            For y = 0 To 15

                tempcalc = 0
                For u = 0 To 15
                    For v = 0 To 15

                        tempcalc = tempcalc + (coefficients(matrixno, u, v) * a(u) * a(v)) * _
                            Cos((((2 * x) + 1) * u * pi) / 32) * _
                            Cos((((2 * y) + 1) * v * pi) / 32)

                    Next v
                Next u

                pixelvaluesY(x, y) = tempcalc / 8

            Next y
        Next x
    End Sub

    Public Function getr(longcolor As Long) As Integer
        Dim intred As Integer
        Dim intgreen As Integer
        Dim intblue As Integer
        Dim lngColor As Long

        lngColor = longcolor

        getr = lngColor Mod &H100

    End Function

    Public Function getg(longcolor As Long) As Integer
        Dim intred As Integer
        Dim intgreen As Integer
        Dim intblue As Integer
        Dim lngColor As Long

        lngColor = longcolor

        lngColor = lngColor \ &H100
        getg = lngColor Mod &H100

    End Function

    Public Function getb(longcolor As Long) As Integer
        Dim intred As Integer
        Dim intgreen As Integer
        Dim intblue As Integer
        Dim lngColor As Long

        lngColor = longcolor
        lngColor = lngColor \ &H100
        lngColor = lngColor \ &H100

        getb = lngColor Mod &H100

    End Function

    Public Function gety(longcolor As Long) As Integer

        gety = (0.299 * getr(longcolor)) + (0.5815 * getg(longcolor)) + (0.114 * getb(longcolor))

    End Function

    Public Function getu(longcolor As Long) As Integer

        getu = (-0.1415 * getr(longcolor)) - (0.289 * getg(longcolor)) + (0.436 * getb(longcolor))

    End Function

    Public Function getv(longcolor As Long) As Integer

        getv = (0.615 * getr(longcolor)) - (0.515 * getg(longcolor)) - (0.1 * getb(longcolor))

    End Function

    Public Function yuvtor(y As Double, v As Double) As Integer

        yuvtor = y + (1.14 * v)

        If yuvtor < 0 Then yuvtor = 0

    End Function

    Public Function yuvtog(y As Double, u As Double, v As Double) As Integer

        yuvtog = y - (0.395 * u) - (0.581 * v)

        If yuvtog < 0 Then yuvtog = 0

    End Function

    Public Function yuvtob(y As Double, u As Double) As Integer

        yuvtob = y + (2.032 * u)

        If yuvtob < 0 Then yuvtob = 0

    End Function



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x As Integer
        Dim y As Integer
        getpixels()
        doDCT(3)

        For x = 0 To 15
            For y = 0 To 15
                pixelvaluesY(x, y) = 0 'clear buffer to prove its empty
            Next y
        Next x
    End Sub

    Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'bit = New Bitmap(PictureBox1.Width, PictureBox1.Height)


    End Sub

    Private Sub Print_DCT()
        Dim I As Integer, J As Integer, Output As String
        DataGridView1.ColumnCount = 16

        For I = 0 To 15
            DataGridView1.Rows.Add()
            Output = coefficients(3, 0, I)
            DataGridView1.Rows(I).Cells(0).Value = Output
            For J = 0 To 15
                Output = coefficients(3, J, I)
                DataGridView1.Rows(I).Cells(J).Value = Output
            Next J
            'Output &= ControlChars.Lf
            'Label1.Text &= Output
        Next I

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim g As Graphics = Graphics.FromImage(CType(NonIndexedImage, Image))

        g.DrawImage(bit, 0, 0)
        g.Dispose()
        putpixels()

        NonIndexedImage.Save("D:\Test Projects\New\image compare\xxxI12P103415021-0001.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub
End Class